package com.surveypark.listener;

import javax.annotation.Resource;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.surveypark.service.LogService;
import com.surveypark.util.LogUtil;

@SuppressWarnings("rawtypes")
@Component
public class IniLogTablesListener implements ApplicationListener {

	@Resource(name = "logService")
	private LogService logService;

	@Override
	public void onApplicationEvent(ApplicationEvent arg0) {

		// 如果监听的是上下文刷新事件表示spring容器已经准备好了
		if (arg0 instanceof ContextRefreshedEvent) {
			
			String tableName = LogUtil.generateLogTabName(0);//生成当前月份表名
			String sql = "create table if not exists " + tableName + " like logs";
			logService.executeSQL(sql);
			
			sql = "create table if not exists " + LogUtil.generateLogTabName(1) + " like logs";//生成下个月的表
			logService.executeSQL(sql);
			
			sql = "create table if not exists " + LogUtil.generateLogTabName(2) + " like logs";//生成下下个月的表
			logService.executeSQL(sql);
			
			System.out.println("日志表 -" + tableName + "-初始化完成");
		}
	}
}
